<% 
'-----------------------------------------------------------------------------------
Sub ChangeItemInCart(iItemID, iItemCount, iItemPrice)
	if iItemID="" then
	else
 	
 		If dictCart.Exists(iItemID) Then
			if iItemCount=0 Then 
				dictCart.Remove iItemID
				'************************
 				If dictPrice.Exists(iItemID) Then	dictPrice.Remove iItemID  
				'************************
			Else
				dictCart(iItemID) = iItemCount
				dictPrice(iItemID) = iItemPrice '***************************
			End If
		Else
			dictCart.Add iItemID, iItemCount
			'*********************************
 			If dictPrice.Exists(iItemID) Then
				dictPrice(iItemID) = iItemPrice
			Else
				dictPrice.Add iItemID, iItemPrice
			End If
			'*********************************
		End If
	End If
	call CountCart()
End Sub
'-----------------------------------------------------------------------------------
Sub AddItemToCart(iItemID, iItemCount, iItemPrice)
	if iItemID="" then
	else
	 	If dictCart.Exists(iItemID) Then
			dictCart(iItemID) = dictCart(iItemID) + iItemCount
		Else
			dictCart.Add iItemID, iItemCount
		End If
	  '*********************************
	 	If dictPrice.Exists(iItemID) Then
			dictPrice(iItemID) = iItemPrice
		Else
			dictPrice.Add iItemID, iItemPrice
		End If
	  '*********************************
	end if
	call countcart()
End Sub
'-----------------------------------------------------------------------------------
Sub RemoveItemFromCart(iItemID, iItemCount)
 	If dictCart.Exists(iItemID) Then
		If dictCart(iItemID) <= iItemCount Then
			dictCart.Remove iItemID
			'************************
			 If dictPrice.Exists(iItemID) Then	dictPrice.Remove iItemID  
			'************************
		Else
			dictCart(iItemID) = dictCart(iItemID) - iItemCount
		End If
	Else
		Response.Write "No se ha podido encontrar el producto en su pedido.<BR><BR>" & vbCrLf
	End If
	
	
	call countcart()
End Sub
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
Function GetItemParameters(iItemID) ',itemPrice)
SQL = "SELECT * FROM Articulos WHERE ID like '%" & iItemID & "%'"

rs.Open SQL, Conn
rs.MoveFirst

Dim aParameters
price=rs("PVP")
descr=rs("Articulo")
prod=rs("Marca")
price_a=rs("price_a")
if price_a="" Then price_a=0
price_a=price_a*vat
price=price-((price*discount)/100)
'if not(isnull(itemPrice)) then price=itemPrice
aParameters = Array(prod, descr, price, price_a)

GetItemParameters = aParameters
rs.close
End Function
'-----------------------------------------------------------------------------------

Dim dictCart ' as dictionary
Dim dictPrice ' as dictionary
Dim sAction ' as string
Dim iItemID ' 
Dim iItemCount ' as integer

If IsObject(Session("cart")) Then
	Set dictCart = Session("cart")
Else
	Set dictCart = Server.CreateObject("Scripting.Dictionary")
End If
If IsObject(Session("cartPrice")) Then
	Set dictPrice = Session("cartPrice")
Else
	Set dictPrice = Server.CreateObject("Scripting.Dictionary")
End If

sAction = CStr(Request.QueryString("action"))
sStep = CStr(Request.QueryString("step"))
iItemID = CStr(Request.QueryString("item"))
if isEmpty(Request.QueryString("count")) OR isnull(Request.QueryString("count")) OR Request.QueryString("count")=""  then
	iItemCount = 0
Else
	iItemCount = CInt(Request.QueryString("count"))
End if
if isEmpty(Request.QueryString("price")) OR isnull(Request.QueryString("price")) OR Request.QueryString("price")=""  then
	aParameters = GetItemParameters(iItemID)
	iItemPrice = aParameters(2)
Else
	iItemPrice = CSng(Request.QueryString("Price"))
End if

'Response.write Request.ServerVariables("HTTP_REFERER") & "<br>"

Select Case sAction
	Case "add"
		AddItemToCart iItemID, iItemCount, iItemPrice
		ShowItemsInCart
		Response.redirect(topage)
		if qty>0 Then 
		End If
   
   Case "change"	
		iItemID=request.form("code")
		iItemCount=request.form("qty")
		iItemPrice=Request.Form("price")
		if iItemCount="" Then iItemCount=0
		ChangeItemInCart iItemID, iItemCount, iItemPrice
		ShowItemsInCart
		Response.redirect(topage)

   Case "changePrice"	
		iItemID=request.form("code")
		iItemPrice=request.form("price")
		if iItemPrice="" Then iItemPrice=0
		ChangeItemInCartPrice iItemID, iItemPrice
		ShowItemsInCart
		Response.redirect(topage)

	Case "del"
	
		RemoveItemFromCart iItemID, iItemCount
		ShowItemsInCart
		Response.redirect(topage)
		If qty>0 Then 
		End if
	Case "viewcart"
		ShowItemsInCart
	'	if qty>0 Then .........
	
   	'	if Session("Total") >= 30 OR Session("Total") <= 0  Then '..............
	
	
	
	case "checkout"

	ShowItemsInCart
	'if Session("logonid") = "" Then Response.redirect(mainpage&"?sid="& sid & "&action=login")
	''Response.write ":" & Request("typo") & ":<br>" 
	'typo=Request("typo")

End Select


'response.write time()-s1 
'Set dictCart = nothing
'Set dictPrice = nothing
Set Session("cart") = dictCart
Set Session("cartPrice") = dictPrice

%>